<?php

class WithdrawModel extends BaseModel
{
    var $table = 'withdraw';
    var $prikey = 'id';
    var $_name = 'withdraw';
    var $_relation = array(
        'has_member' => array(
            'model' => 'member',
            'type' => BELONGS_TO,
            'foreign_key' => 'user_id',
            'reverse' => 'user_withdraw',
        ),
    );

    // 用常量的方式定义支持的支付类型、申请状态
    // 微信
    const TYPE_WXPAY = '1';
    // 支付宝
    const TYPE_ALIPAY = '2';

    //提现申请中
    const STATUS_ING = 1;
    //提现申请通过
    const STATUS_ADOPT = 2;
    //提现申请失败
    const STATUS_REFUSE = 3;
    //提现打款成功
    const STATUS_SUCCESS = 4;
    //提现打款失败
    const STATUS_ERROR = 5;

    // 设置映射关系
    public static $typeMap = array(
        self::TYPE_WXPAY => '微信',
        self::TYPE_ALIPAY => '支付宝',
    );
    // 设置映射关系
    public static $statusMap = array(
        self::STATUS_ING => '提现申请中',
        self::STATUS_ADOPT => '提现申请已通过',
        self::STATUS_REFUSE => '提现申请被拒绝',
        self::STATUS_SUCCESS => '打款成功',
        self::STATUS_ERROR => '打款失败',
    );

    static function aliTransfer($data){
        ini_set('display_errors','on');
        error_reporting(E_ALL);

        include_once ROOT_PATH."/includes/payments/alipay/aop/AopCertClient.php";
        include_once ROOT_PATH."/includes/payments/alipay/aop/request/AlipayFundTransUniTransferRequest.php";

        $appCertPath    = ALI_APP_CERT_PATH;
        $alipayCertPath = ALI_ALI_PAY_CERT_PATH;
        $rootCertPath   = ALI_ROOT_CERT_PATH;


        $aop = new AopCertClient();
        $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
        $aop->appId = ALI_APP_ID;
        $aop->rsaPrivateKey = ALI_RSA_PRIVATE_KEY;
        $aop->alipayrsaPublicKey = $aop->getPublicKey($alipayCertPath);
        $aop->apiVersion = '1.0';
        $aop->signType = 'RSA2';
        $aop->postCharset='UTF-8';
        $aop->format='json';
        $aop->isCheckAlipayPublicCert = true;//是否校验自动下载的支付宝公钥证书，如果开启校验要保证支付宝根证书在有效期内
        $aop->appCertSN = $aop->getCertSN($appCertPath);//调用getCertSN获取证书序列号
        $aop->alipayRootCertSN = $aop->getRootCertSN($rootCertPath);//调用getRootCertSN获取支付宝根证书序列号

        $request = new AlipayFundTransUniTransferRequest();
        $request->setBizContent("{" .
            "\"out_biz_no\":".'"'.$data['sn'].'"'."," .
            "\"trans_amount\":".'"'.$data['money'].'"'."," .
            "\"product_code\":\"TRANS_ACCOUNT_NO_PWD\"," .
            "\"biz_scene\":\"DIRECT_TRANSFER\"," .
            "\"order_title\":\"提现\"," .
            "\"payee_info\":{" .
            "\"identity\":".'"'.$data['number'].'"'."," .
            "\"identity_type\":\"ALIPAY_LOGON_ID\"," .
            "\"name\":".'"'.$data['user_name'].'"'."" .
            "    }," .
            "\"remark\":\"现金提现\"," .
            "\"business_params\":\"{\\\"sub_biz_scene\\\":\\\"REDPACKET\\\"}\"" .
            "  }");
        $result = $aop->execute($request);
        $res = json_decode(json_encode($result),true);
        return $res;
    }

    static function wxTransfer($data){
        ini_set('display_errors','on');
        error_reporting(E_ALL);
        include_once ROOT_PATH."/includes/payments/epaywxnative/lib/WxMchPay.php";
        $mchPay = new WxMchPay();
        // 用户openid
        $mchPay->setParameter('openid', $data['openid']);
        // 商户订单号
        $mchPay->setParameter('partner_trade_no',$data['sn']);
        // 校验用户姓名选项
        $mchPay->setParameter('check_name', 'NO_CHECK');
        // 企业付款金额  单位为分
        $mchPay->setParameter('amount', $data['money']*100);
        // 企业付款描述信息
        $mchPay->setParameter('desc', '企业打款');
        // 调用接口的机器IP地址  自定义
        $mchPay->setParameter('spbill_create_ip', '127.0.0.1'); # getClientIp()
        // 收款用户姓名
        // $mchPay->setParameter('re_user_name', 'Max wen');
        // 设备信息
        // $mchPay->setParameter('device_info', 'dev_server');
        $xml = $mchPay->createXml();
        $url = 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers';
        $response = $mchPay->postXmlSSLCurl($xml,$url);
        if( !empty($response) ) {
            $data = simplexml_load_string($response, null, LIBXML_NOCDATA);
            return json_decode(json_encode($data),true);
        }else{
            return array('return_code'=>"FAIL",'return_msg'=>'transfers_接口出错');
        }

    }
}